package com.giscloud.amp.service.auth.impl;

import com.alibaba.fastjson.JSONObject;
import com.giscloud.amp.dao.ApKeyRuleRelationMapper;
import com.giscloud.amp.dao.ApLogMapper;
import com.giscloud.amp.dao.ApRuleMatchMapper;
import com.giscloud.amp.entity.*;
import com.giscloud.amp.service.auth.ApKeyRuleRelationService;

import com.giscloud.commons.utils.StringUtils;
import com.giscloud.datasource.page.PageInfo;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

@Service
@Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
public class ApKeyRuleRelationServiceImpl implements ApKeyRuleRelationService {

    private final static Long ENABlE_TYPE = 1L;
    private final static Long DISABLE_TYPE = 2L;
    private final static Long UPDATE_ORDER_TYPE = 3L;


    @Autowired
    private ApKeyRuleRelationMapper apKeyRuleRelationMapper;

    @Autowired
    private ApLogMapper apLogMapper;

    @Autowired
    private ApRuleMatchMapper apRuleMatchMapper;


    @Override
    public PageInfo selectByExample(Map<String, Object> params) {
        String cityName = params.get("cityName") == null?"":String.valueOf(params.get("cityName"));
        String keyCode = params.get("keyCode") == null?"":String.valueOf(params.get("keyCode"));
        String ruleName = params.get("ruleName") == null?"":String.valueOf(params.get("ruleName"));
        String ruleLevel = params.get("ruleLevel") == null?"":String.valueOf(params.get("ruleLevel"));
        ApRuleMatchExample apRuleMatchExample = new ApRuleMatchExample();
        ApRuleMatchExample.Criteria apRuleMatchExampleCriteria = apRuleMatchExample.createCriteria();
        apRuleMatchExampleCriteria.andStatusCdEqualTo(BigDecimal.valueOf(1L));
        if(StringUtils.isNotBlank(ruleName)){
            apRuleMatchExampleCriteria.andRuleNameLike("%" + ruleName + "%");
        }
        if(StringUtils.isNotBlank(ruleLevel)){
            apRuleMatchExampleCriteria.andRuleLevelEqualTo(BigDecimal.valueOf(Long.valueOf(ruleLevel)));

        }
        List<ApRuleMatch> apRuleMatchList = apRuleMatchMapper.selectByExample(apRuleMatchExample);
        if(CollectionUtils.isEmpty(apRuleMatchList)){
            return PageInfo.of(new ArrayList<ApRuleMatch>());
        }
        ApKeyRuleRelationExample apKeyRuleRelationExample = new ApKeyRuleRelationExample();
        ApKeyRuleRelationExample.Criteria apKeyRuleRelationCriteria = apKeyRuleRelationExample.createCriteria();
        apKeyRuleRelationCriteria.andKeyCodeEqualTo(keyCode);
        apKeyRuleRelationCriteria.andCityNameEqualTo(cityName);
        List<BigDecimal> ruleIdList = new ArrayList<BigDecimal>();
        for(ApRuleMatch item : apRuleMatchList){
            ruleIdList.add(item.getRuleId());
        }
        apKeyRuleRelationCriteria.andRuleIdIn(ruleIdList);
        List<ApKeyRuleRelation> apKeyRuleRelationList = apKeyRuleRelationMapper.selectByExample(apKeyRuleRelationExample);
        for(ApRuleMatch apRuleMatch : apRuleMatchList){
            for(ApKeyRuleRelation apKeyRuleRelation : apKeyRuleRelationList) {
                if (apRuleMatch.getRuleId().equals(apKeyRuleRelation.getRuleId())) {
                    apRuleMatch.setCityId(apKeyRuleRelation.getCityId());
                    apRuleMatch.setCityName(apKeyRuleRelation.getCityName());
                    apRuleMatch.setRelationId(apKeyRuleRelation.getRelationId());
                    apRuleMatch.setRelationDispOrder(apKeyRuleRelation.getDispOrder());
                    apRuleMatch.setRelationStatusCd(apKeyRuleRelation.getStatusCd());
                    apRuleMatch.setRelationCreatedDt(apKeyRuleRelation.getCreatedDt());
                    apRuleMatch.setUseNumber(apKeyRuleRelation.getUseNumber());
                }
            }
        }
        List<ApRuleMatch> resultList = new ArrayList<ApRuleMatch>();
        if(params.get("relationStatusCd") == null || "".equals(params.get("relationStatusCd"))){
            resultList = apRuleMatchList;
        }else{
            for(ApRuleMatch apRuleMatch : apRuleMatchList){
                if("1".equals(params.get("relationStatusCd")) ){
                    if( apRuleMatch.getRelationStatusCd() != null
                            && apRuleMatch.getRelationStatusCd().intValue() == 1){
                        resultList.add(apRuleMatch);
                    }
                }else{
                    if( apRuleMatch.getRelationStatusCd() == null
                            || apRuleMatch.getRelationStatusCd().intValue() != 1){
                        resultList.add(apRuleMatch);
                    }
                }
            }
        }

        Collections.sort(resultList, new Comparator<ApRuleMatch>() {
            @Override
            public int compare(ApRuleMatch o1, ApRuleMatch o2) {
                if(o1.getRelationDispOrder() == null && o2.getRelationDispOrder() != null){
                    return 1;
                }
                if(o2.getRelationDispOrder() == null && o1.getRelationDispOrder() != null ){
                    return -1;
                }
                if(o2.getRelationDispOrder() == null && o1.getRelationDispOrder() == null ){
                   return 0;
                }
//                if(o1.getRelationDispOrder().compareTo(o2.getRelationDispOrder()) == 0){
//                    return o1.getRelationCreatedDt().compareTo(o2.getRelationCreatedDt());
//                }
                return o1.getRelationDispOrder().compareTo(o2.getRelationDispOrder());
            }
        });
        return PageInfo.of(resultList);
    }


    @Override
    public List<ApKeyRuleRelation> getCityList(Map<String, Object> params) {
        List<Long> list = new ArrayList<Long>();
        if(params.get("cityIds") != null && !"".equals(params.get("cityIds"))){
            String cityIds = String.valueOf(params.get("cityIds"));
            for(String cityId : cityIds.split("[,，]")){
                list.add(Long.valueOf(cityId));
            }

        }
        return apKeyRuleRelationMapper.getCityNameList(list);
    }

    @Override
    public int insertSelective(ApKeyRuleRelation record) {
        record.setCreatedDt(new Date());
        record.setStatusCd(BigDecimal.valueOf(1L));
        record.setDispOrder(BigDecimal.valueOf(1L));
        ApKeyRuleRelationExample apKeyRuleRelationExample = new ApKeyRuleRelationExample();
        ApKeyRuleRelationExample.Criteria apKeyRuleRelationCriteria = apKeyRuleRelationExample.createCriteria();
        apKeyRuleRelationCriteria.andKeyCodeEqualTo(record.getKeyCode());
        apKeyRuleRelationCriteria.andCityNameEqualTo(record.getCityName());
        apKeyRuleRelationCriteria.andStatusCdEqualTo(BigDecimal.valueOf(1L));
        apKeyRuleRelationExample.setOrderByClause("disp_order desc");
        List<ApKeyRuleRelation> apKeyRuleRelationList = apKeyRuleRelationMapper.selectByExample(apKeyRuleRelationExample);
        if(CollectionUtils.isNotEmpty(apKeyRuleRelationList)){
             record.setDispOrder(BigDecimal.valueOf(apKeyRuleRelationList.get(0).getDispOrder().longValue() + 1));
        }
        if(record.getRelationId() != null){
            apKeyRuleRelationMapper.updateByPrimaryKey(record);
        }else{
            apKeyRuleRelationMapper.insertSelective(record);
        }
        recordLog(ENABlE_TYPE,record.getCreatedStaffId(),record.getUserName(),record.getKeyCode(),record.getCityName(),record.getRelationId().longValue(),record.getRuleId().longValue(),JSONObject.toJSONString(record));
       // recordLog(ENABlE_TYPE,record.getCreatedStaffId(),record.getKeyCode(),JSONObject.toJSONString(record));
        return 1;
    }

    @Override
    public int deleteByPrimaryKey(ApKeyRuleRelation record) {
        ApKeyRuleRelation apKeyRuleRelation = apKeyRuleRelationMapper.selectByPrimaryKey(record.getRelationId());
        if(apKeyRuleRelation == null || apKeyRuleRelation.getRelationId() == null){
            return 0;
        }
        apKeyRuleRelation.setStatusCd(BigDecimal.valueOf(0L));
        apKeyRuleRelation.setDispOrder(null);
        apKeyRuleRelationMapper.updateByPrimaryKey(apKeyRuleRelation);
        //apKeyRuleRelationMapper.deleteByPrimaryKey(BigDecimal.valueOf(relationId));
        recordLog(DISABLE_TYPE,record.getCreatedStaffId(),record.getUserName(),record.getKeyCode(),record.getCityName(),record.getRelationId().longValue(),apKeyRuleRelation.getRuleId().longValue(),"");

        return 1;
    }


    @Override
    public int updateByPrimaryKeySelective(ApKeyRuleRelation record) {
        record.setStatusCd(BigDecimal.valueOf(1L));
        ApKeyRuleRelationExample apKeyRuleRelationExample = new ApKeyRuleRelationExample();
        ApKeyRuleRelationExample.Criteria apKeyRuleRelationCriteria = apKeyRuleRelationExample.createCriteria();
        apKeyRuleRelationCriteria.andKeyCodeEqualTo(record.getKeyCode());
        apKeyRuleRelationCriteria.andCityNameEqualTo(record.getCityName());
        apKeyRuleRelationCriteria.andStatusCdEqualTo(BigDecimal.valueOf(1L));
        apKeyRuleRelationCriteria.andDispOrderEqualTo(record.getDispOrder());
        apKeyRuleRelationCriteria.andRelationIdNotEqualTo(record.getRelationId());
        List<ApKeyRuleRelation> apKeyRuleRelationList = apKeyRuleRelationMapper.selectByExample(apKeyRuleRelationExample);
        if(CollectionUtils.isNotEmpty(apKeyRuleRelationList)){
            apKeyRuleRelationExample = new ApKeyRuleRelationExample();
            apKeyRuleRelationCriteria = apKeyRuleRelationExample.createCriteria();
            apKeyRuleRelationCriteria.andKeyCodeEqualTo(record.getKeyCode());
            apKeyRuleRelationCriteria.andCityNameEqualTo(record.getCityName());
            apKeyRuleRelationCriteria.andStatusCdEqualTo(BigDecimal.valueOf(1L));
            apKeyRuleRelationCriteria.andDispOrderGreaterThanOrEqualTo(record.getDispOrder());
            apKeyRuleRelationMapper.updateDispOrderBatch(record,apKeyRuleRelationExample);
        }
        apKeyRuleRelationMapper.updateByPrimaryKeySelective(record);
        recordLog(UPDATE_ORDER_TYPE,record.getCreatedStaffId(),record.getUserName(),record.getKeyCode(),record.getCityName(),record.getRelationId().longValue(),record.getRuleId().longValue(),JSONObject.toJSONString(record));

        return 1;
    }

    @Override
    public int upOrDownOrder(Map<String, Object> params) {
        ApKeyRuleRelation record = new ApKeyRuleRelation();
        String cityName = String.valueOf(params.get("cityName"));
        String keyCode = String.valueOf(params.get("keyCode"));
        String userId = String.valueOf(params.get("userId"));
        String userName = String.valueOf(params.get("userName"));
        record.setCityName(cityName);
        record.setKeyCode(keyCode);
        record.setCreatedStaffId(userId);
        String relationId1 = String.valueOf(params.get("relationId1"));
        String ruleId1 = String.valueOf(params.get("ruleId1"));
        String dispOrder1 = String.valueOf(params.get("dispOrder1"));
        String relationId2 = String.valueOf(params.get("relationId2"));
        String ruleId2 = String.valueOf(params.get("ruleId2"));
        String dispOrder2 = String.valueOf(params.get("dispOrder2"));
        record.setRelationId(BigDecimal.valueOf(Long.valueOf(relationId1)));
        record.setDispOrder(BigDecimal.valueOf(Long.valueOf(dispOrder2)));
        apKeyRuleRelationMapper.updateByPrimaryKeySelective(record);
        recordLog(UPDATE_ORDER_TYPE,record.getCreatedStaffId(),userName,record.getKeyCode(),record.getCityName(),record.getRelationId().longValue(),Long.valueOf(ruleId1),JSONObject.toJSONString(record));
        record.setRelationId(BigDecimal.valueOf(Long.valueOf(relationId2)));
        record.setDispOrder(BigDecimal.valueOf(Long.valueOf(dispOrder1)));
        apKeyRuleRelationMapper.updateByPrimaryKeySelective(record);
        recordLog(UPDATE_ORDER_TYPE,record.getCreatedStaffId(),userName,record.getKeyCode(),record.getCityName(),record.getRelationId().longValue(),Long.valueOf(ruleId2),JSONObject.toJSONString(record));
        return 1;
    }

    @Override
    public PageInfo selectLogByExample(Map<String, Object> params) {
        ApLogExample apLogExample = new ApLogExample();
        ApLogExample.Criteria criteria = apLogExample.createCriteria();
        if(params.get("cityName") != null ){
            criteria.andText1EqualTo(String.valueOf(params.get("cityName")));
        }
        if(params.get("relationId") != null && !"".equals(params.get("relationId") )){
            String relationId = String.valueOf(params.get("relationId"));
            criteria.andNumber1EqualTo(BigDecimal.valueOf(Long.valueOf(relationId)));
        }
        if(params.get("ruleId") != null && !"".equals(params.get("ruleId") ) ){
            String ruleId = String.valueOf(params.get("ruleId"));
            criteria.andNumber2EqualTo(BigDecimal.valueOf(Long.valueOf(ruleId)));
        }
        if(params.get("userName") != null ){
            criteria.andCreatedStaffNameEqualTo(String.valueOf(params.get("userName")));
        }
        if(params.get("userId") != null ){
            criteria.andCreatedStaffIdEqualTo(String.valueOf(params.get("userId")));
        }
        if(params.get("keyCode") != null ){
            criteria.andKeyCodeEqualTo(String.valueOf(params.get("keyCode")));
        }
        apLogExample.setOrderByClause("created_dt desc");
        PageHelper.startPage((int)params.get("page"),(int)params.get("rows"));
        List<ApLog> list = apLogMapper.selectByExample(apLogExample);
        return PageInfo.of(list);
    }

    @Override
    public PageInfo selectRuleList(Map<String, Object> params) {
        String ruleName = params.get("ruleName") == null?"":String.valueOf(params.get("ruleName"));
        String ruleLevel = params.get("ruleLevel") == null?"":String.valueOf(params.get("ruleLevel"));
        String ruleId = params.get("ruleId") == null?"":String.valueOf(params.get("ruleId"));
        String keyCode = params.get("keyCode") == null?"":String.valueOf(params.get("keyCode"));
        ApRuleMatchExample apRuleMatchExample = new ApRuleMatchExample();
        ApRuleMatchExample.Criteria apRuleMatchExampleCriteria = apRuleMatchExample.createCriteria();
        apRuleMatchExampleCriteria.andStatusCdEqualTo(BigDecimal.valueOf(1L));
        apRuleMatchExample.setOrderByClause("rule_id desc");
        if(StringUtils.isNotBlank(ruleName)){
            apRuleMatchExampleCriteria.andRuleNameLike("%" + ruleName + "%");
        }
        if(StringUtils.isNotBlank(ruleLevel)){
            apRuleMatchExampleCriteria.andRuleLevelEqualTo(BigDecimal.valueOf(Long.valueOf(ruleLevel)));
        }
        if(StringUtils.isNotBlank(ruleId)){
            apRuleMatchExampleCriteria.andRuleIdEqualTo(BigDecimal.valueOf(Long.valueOf(ruleId)));
        }
        List<ApRuleMatch> apRuleMatchList = apRuleMatchMapper.selectByExample(apRuleMatchExample);
        if(CollectionUtils.isEmpty(apRuleMatchList)){
            return PageInfo.of(new ArrayList<ApRuleMatch>());
        }
        ApKeyRuleRelationExample apKeyRuleRelationExample = new ApKeyRuleRelationExample();
        ApKeyRuleRelationExample.Criteria apKeyRuleRelationCriteria = apKeyRuleRelationExample.createCriteria();
        List<BigDecimal> ruleIdList = new ArrayList<BigDecimal>();
        for(ApRuleMatch item : apRuleMatchList){
            ruleIdList.add(item.getRuleId());
        }
        apKeyRuleRelationCriteria.andRuleIdIn(ruleIdList);
        if(StringUtils.isNotBlank(keyCode)){
            apKeyRuleRelationCriteria.andKeyCodeEqualTo(keyCode);
        }
        List<ApKeyRuleRelation> apKeyRuleRelationList = apKeyRuleRelationMapper.selectByExample(apKeyRuleRelationExample);
        for(ApRuleMatch apRuleMatch : apRuleMatchList){
            apRuleMatch.setUseNumber(BigDecimal.valueOf(0));
            apRuleMatch.setCityCnt(0);
            if(CollectionUtils.isNotEmpty(apKeyRuleRelationList)){
                for(ApKeyRuleRelation apKeyRuleRelation : apKeyRuleRelationList) {
                    if (apRuleMatch.getRuleId().equals(apKeyRuleRelation.getRuleId())) {
                        if (apKeyRuleRelation.getUseNumber() != null){
                            apRuleMatch.setUseNumber(BigDecimal.valueOf(apKeyRuleRelation.getUseNumber().longValue() + apRuleMatch.getUseNumber().longValue()));
                        }
                        if(apKeyRuleRelation.getStatusCd()!=null && apKeyRuleRelation.getStatusCd().intValue() == 1){
                            apRuleMatch.setCityCnt(apRuleMatch.getCityCnt() + 1);
                        }
                    }
                }
            }

        }
        return PageInfo.of(apRuleMatchList);
    }

    /**
     * 批量启用规则
     * @param params
     * @return
     */
    @Override
    public int batchEnableRule(Map<String, Object> params) {
        BigDecimal ruleId = BigDecimal.valueOf(Long.valueOf(String.valueOf(params.get("ruleId"))));
        String keyCode = String.valueOf(params.get("keyCode"));
        String userId = String.valueOf(params.get("userId"));
        String userName = String.valueOf(params.get("userName"));
        List<Long> list = new ArrayList<Long>();
        String cityIds = String.valueOf(params.get("cityId"));
        if(!"0".equals(cityIds)){
            for(String cityId : cityIds.split("[,，]")){
                list.add(Long.valueOf(cityId));
            }
        }
        List<ApKeyRuleRelation> cityList = apKeyRuleRelationMapper.getCityNameList(list);
        for(ApKeyRuleRelation cityItem : cityList){
            BigDecimal cityId = cityItem.getCityId();
            ApKeyRuleRelation record = new ApKeyRuleRelation();
            record.setCreatedDt(new Date());
            record.setStatusCd(BigDecimal.valueOf(1L));
            record.setDispOrder(BigDecimal.valueOf(1L));
            record.setCityName(cityItem.getCityName());
            record.setCityId(cityId);
            record.setKeyCode(keyCode);
            record.setCreatedStaffId(userId);
            record.setUserName(userName);
            record.setRuleId(ruleId);
            ApKeyRuleRelationExample apKeyRuleRelationExample = new ApKeyRuleRelationExample();
            ApKeyRuleRelationExample.Criteria apKeyRuleRelationCriteria = apKeyRuleRelationExample.createCriteria();
            apKeyRuleRelationCriteria.andKeyCodeEqualTo(keyCode);
            apKeyRuleRelationCriteria.andCityIdEqualTo(cityId);
            apKeyRuleRelationExample.setOrderByClause("disp_order desc");
            List<ApKeyRuleRelation> apKeyRuleRelationList = apKeyRuleRelationMapper.selectByExample(apKeyRuleRelationExample);
            if(CollectionUtils.isNotEmpty(apKeyRuleRelationList)){
                for(ApKeyRuleRelation item : apKeyRuleRelationList){
                    if(record.getDispOrder().intValue() == 1 && item.getDispOrder() != null){
                        record.setDispOrder(BigDecimal.valueOf(item.getDispOrder().longValue() + 1));
                    }
                    if(item.getRuleId().equals(ruleId) &&item.getCityId().equals(cityId) && item.getKeyCode().equals(keyCode)){
                        record.setRelationId(item.getRelationId());
                        if(item.getDispOrder() != null){
                            record.setDispOrder(item.getDispOrder());
                        }
                    }
                }
            }
            if(record.getRelationId() != null){
                apKeyRuleRelationMapper.updateByPrimaryKey(record);
            }else{
                apKeyRuleRelationMapper.insertSelective(record);
            }
            recordLog(ENABlE_TYPE,record.getCreatedStaffId(),record.getUserName(),record.getKeyCode(),record.getCityName(),record.getRelationId().longValue(),record.getRuleId().longValue(),JSONObject.toJSONString(record));

        }
        return 0;
    }

    /**
     * 批量禁用规则
     * @param params
     * @return
     */
    @Override
    public int batchDisableRule(Map<String, Object> params) {
        ApKeyRuleRelationExample apKeyRuleRelationExample = new ApKeyRuleRelationExample();
        ApKeyRuleRelationExample.Criteria apKeyRuleRelationCriteria = apKeyRuleRelationExample.createCriteria();
        String ruleId = String.valueOf(params.get("ruleId"));
        String keyCode = String.valueOf(params.get("keyCode"));
        String userId = String.valueOf(params.get("userId"));
        String userName = String.valueOf(params.get("userName"));
        apKeyRuleRelationCriteria.andRuleIdEqualTo(BigDecimal.valueOf(Long.valueOf(ruleId)));
        apKeyRuleRelationCriteria.andKeyCodeEqualTo(keyCode);
        if(params.get("cityId") != null && !"".equals(params.get("cityId"))){
            List<BigDecimal> list = new ArrayList<BigDecimal>();
            String cityIds = String.valueOf(params.get("cityId"));
            if(!"0".equals(cityIds)){
                for(String cityId : cityIds.split("[,，]")){
                    list.add(BigDecimal.valueOf(Long.valueOf(cityId)));
                }
                apKeyRuleRelationCriteria.andCityIdIn(list);
            }
        }
        List<ApKeyRuleRelation> apKeyRuleRelationList = apKeyRuleRelationMapper.selectByExample(apKeyRuleRelationExample);
        for(ApKeyRuleRelation apKeyRuleRelation : apKeyRuleRelationList){
            apKeyRuleRelation.setStatusCd(BigDecimal.valueOf(0L));
            apKeyRuleRelation.setDispOrder(null);
            apKeyRuleRelationMapper.updateByPrimaryKey(apKeyRuleRelation);
            //apKeyRuleRelationMapper.deleteByPrimaryKey(BigDecimal.valueOf(relationId));
            recordLog(DISABLE_TYPE,userId,userName,apKeyRuleRelation.getKeyCode(),apKeyRuleRelation.getCityName(),apKeyRuleRelation.getRelationId().longValue(),apKeyRuleRelation.getRuleId().longValue(),"");

        }
        return 0;
    }

    /**
     * 查询使用此规则的城市列表
     * @param params
     * @return
     */
    @Override
    public List<ApKeyRuleRelation> getCityListByRule(Map<String, Object> params) {
        ApKeyRuleRelationExample apKeyRuleRelationExample = new ApKeyRuleRelationExample();
        ApKeyRuleRelationExample.Criteria apKeyRuleRelationCriteria = apKeyRuleRelationExample.createCriteria();
        if(params.get("ruleId") != null && !"".equals(params.get("ruleId") ) ){
            String ruleId = String.valueOf(params.get("ruleId"));
            apKeyRuleRelationCriteria.andRuleIdEqualTo(BigDecimal.valueOf(Long.valueOf(ruleId)));
        }
        if(params.get("keyCode") != null && !"".equals(params.get("keyCode") ) ){
            String keyCode = String.valueOf(params.get("keyCode"));
            apKeyRuleRelationCriteria.andKeyCodeEqualTo(keyCode);
        }
        if(params.get("cityId") != null && !"".equals(params.get("cityId"))){
            List<BigDecimal> list = new ArrayList<BigDecimal>();
            String cityIds = String.valueOf(params.get("cityId"));
            for(String cityId : cityIds.split("[,，]")){
                list.add(BigDecimal.valueOf(Long.valueOf(cityId)));
            }
            apKeyRuleRelationCriteria.andCityIdIn(list);
        }
        apKeyRuleRelationCriteria.andStatusCdEqualTo(BigDecimal.valueOf(1));
        List<ApKeyRuleRelation> apKeyRuleRelationList = apKeyRuleRelationMapper.selectByExample(apKeyRuleRelationExample);
        List<ApKeyRuleRelation> resultList = new ArrayList<ApKeyRuleRelation>();
        Map<BigDecimal,ApKeyRuleRelation> cityIdMap = new HashMap<BigDecimal,ApKeyRuleRelation>();
        for(ApKeyRuleRelation item : apKeyRuleRelationList){
            if(!cityIdMap.containsKey(item.getCityId())){
                ApKeyRuleRelation resultItem = new ApKeyRuleRelation();
                resultItem.setCityId(item.getCityId());
                resultItem.setCityName(item.getCityName());
                resultItem.setUseNumber(BigDecimal.valueOf(0));
                if(item.getUseNumber() != null){
                    resultItem.setUseNumber(item.getUseNumber());
                }else{
                    resultItem.setUseNumber(BigDecimal.valueOf(0));
                }
                resultList.add(resultItem);
                cityIdMap.put(item.getCityId(),resultItem);
            }else{
                ApKeyRuleRelation resultItem = cityIdMap.get(item.getCityId());
                if(item.getUseNumber() != null){
                    resultItem.setUseNumber(BigDecimal.valueOf(resultItem.getUseNumber().longValue() + item.getUseNumber().longValue()));
                }
            }
        }
        return resultList;
    }

    /**
     * 查询规则信息
     * @param apRuleMatch
     * @return
     */
    @Override
    public ApRuleMatch selectApRule(ApRuleMatch apRuleMatch) {
        return apRuleMatchMapper.selectByPrimaryKey(apRuleMatch.getRuleId());
    }

    /**
     * 修改规则信息
     * @param apRuleMatch
     * @return
     */
    @Override
    public int updateApRule(ApRuleMatch apRuleMatch) {
        ApRuleMatchExample apRuleMatchExample = new ApRuleMatchExample();
        ApRuleMatchExample.Criteria criteria  = apRuleMatchExample.createCriteria();
        criteria.andRuleIdEqualTo(apRuleMatch.getRuleId());
        return apRuleMatchMapper.updateByExampleSelective(apRuleMatch,apRuleMatchExample);
    }

    /**
     * 记录操作日志
     * @param logType
     * @param createdStaffId
     * @param keyCode
     * @param logContent
     */
    private void recordLog(Long logType,String createdStaffId,String createdName,String keyCode,String cityName,Long getRelationId,Long ruleId,String logContent){
        ApLog aplog = new ApLog();
        aplog.setLogType(BigDecimal.valueOf(logType));
        aplog.setCreatedDt(new Date());
        aplog.setCreatedStaffId(createdStaffId);
        aplog.setKeyCode(keyCode);
        aplog.setLogContent(logContent);
        aplog.setText1(cityName);
        aplog.setNumber1(BigDecimal.valueOf(getRelationId));
        aplog.setNumber2(BigDecimal.valueOf(ruleId));
        aplog.setCreatedStaffName(createdName);
        apLogMapper.insertSelective(aplog);
    }
}
